package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentService;

import java.util.Scanner;

public abstract class BaseStudentController {
    Scanner sc = new Scanner(System.in);
    StudentService studentService = new StudentService();

    public void start() {

        studentLoop:
        while (true) {
            System.out.println("==========欢迎来到《学生》管理系统！==========");
            System.out.println("请输入您要进行的操作：");
            System.out.println("1、添加学生\t2、删除学生\t3、修改学生\t4、查看学生\t5、退出学生管理系统");
            String choice = sc.next();

            switch (choice) {
                case "1":
                    //System.out.println("添加学生");
                    boolean result = addStudent();
                    if (result) {
                        System.out.println("添加成功！");
                    }
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleteStudent();
                    break;
                case "3":
                    //System.out.println("修改学生");
                    updateStudent();
                    System.out.println("修改成功！");
                    break;
                case "4":
                    //System.out.println("查看学生");
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("退出学生管理系统，感您的使用！");
                    break studentLoop;
                default:
                    System.out.println("您的输入有误！请重新输入！");
                    break;
            }
        }
    }

    public void updateStudent() {
        String id = inputStudentId();
        Student stu = inputStudentInfo(id);

        studentService.updateStudent(id, stu);
    }

    public String inputStudentId() {
        String id;
        while (true) {
            System.out.println("请输入需要学员学号：");
            id = sc.next();
            boolean result = studentService.isExists(id);
            if (!result) {
                System.out.println("您所查询的学员不存在！请重新输入！");
            } else {
                break;
            }
        }
        return id;
    }

    public void deleteStudent() {
        String id = inputStudentId();
        studentService.deleteStudentById(id);
        System.out.println("删除成功！");
    }

    public void findAllStudent() {
        Student[] stuArr = studentService.findAllStudent();
        if (stuArr == null) {
            System.out.println("查无信息，请添加后重试！");
            return;
        }
        System.out.println("学号\t\t\t姓名\t\t年龄\t\t生日");
        for (int i = 0; i < stuArr.length; i++) {
            Student stu = stuArr[i];
            if (stu != null) {
                System.out.println(stu.getId() + "\t" + stu.getName() + "\t\t" + stu.getAge() + "\t\t" + stu.getBirthday());
            }
        }

    }

    public boolean addStudent() {
        String sid;
        while (true) {
            //需要检查所输入的学号是否存在！
            System.out.println("请输入学生学号：");
            sid = sc.next();
            boolean result = studentService.isExists(sid);
            if (result) {
                System.out.println("您的输入有误！请重新输入！");
            } else {
                break;
            }
        }

        Student stu = inputStudentInfo(sid);

        boolean result = studentService.addStudent(stu);
        return result;
    }

    public abstract Student inputStudentInfo(String sid);

}
